﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using JumbotPay.API.WxPayAPI;

namespace JumbotPay.WebFile.Wxpay_Mobile
{
    public partial class _default_step3 : JumbotPay.UI.BasicPage
    {
        public static string wxJsApiParam { get; set; } //H5调起JS API参数
        public string account_wxpay;
        public string Account_Wxpay_Name;
        public string Order_ID = "";
        public string Product_Name = "";
        public string Product_Desc = "";
        public string Order_Time = "";
        public string Order_Money = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string _openid = q("openid");
            Order_ID = q("order_id");
            account_wxpay = q("account_no");
            WxPayData req = new WxPayData();
            object[] _value = new JumbotPay.DAL.OrderDAL().GetOrderFields("wxpay", Order_ID, "", "product_name,product_desc,pay_amount,Order_Time,account_wxpay_name");
            Product_Name = _value[0].ToString();                            //商品名称
            Product_Desc = _value[1].ToString();                                 //商品描述
            int total_fee = (int)Str2Double(_value[2].ToString())*100;
            Order_Time = Convert.ToDateTime(_value[3].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            Account_Wxpay_Name = _value[4].ToString();
            Order_Money = Str2Double(_value[2].ToString()).ToString("f2");
            string _out_trade_no = new WxPayApi().GenerateOutTradeNo(account_wxpay);
            _out_trade_no = new JumbotPay.DAL.OrderDAL().UpdateOutOrderNO(Order_ID, _out_trade_no);
            try
            {
                JsApiPay jsApiPay = new JsApiPay(this);

                //JSAPI支付预处理
                try
                {
                    //统一下单
                    WxPayData data = new WxPayData();
                    data.SetValue("body", Product_Desc);
                    data.SetValue("attach", Order_ID);
                    data.SetValue("out_trade_no", _out_trade_no);
                    data.SetValue("total_fee", total_fee == 8888800 ? 1 : total_fee);//分
                    data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
                    data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
                    data.SetValue("goods_tag", Product_Name);
                    data.SetValue("trade_type", "JSAPI");
                    data.SetValue("openid", _openid);

                    WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(data, account_wxpay);
                    wxJsApiParam = jsApiPay.GetJsApiParameters(account_wxpay);//获取H5调起JS API参数                    
                    JumbotPay.Utils.Logs.Debug("wxpayM_unifiedOrder", "" + wxJsApiParam, false);
                    //Response.Write("<script>$(document).ready(function() { callpay(); });</script>");

                }
                catch (Exception ex)
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败，请返回重试" + "</span>");
                    JumbotPay.Utils.Logs.Error("wxpay_unifiedOrder_" + account_wxpay, "" + ex, true);
                }
            }
            catch (Exception ex)
            {
                JumbotPay.Utils.Logs.Error("wxpay_GetEditAddressParameters", "" + ex, true);
                Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错，请重试" + "</span>");
            }
            
        }
    }
}
